package com.das.mechanic_base.adapter.common;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;

import com.das.mechanic_base.bean.main.CarQuestionSelectBean;
import com.das.mechanic_base.bean.main.CrmCarQuetionBean;

import java.util.ArrayList;
import java.util.List;

public class X3SelectCrmQuestionAdapter extends RecyclerView.Adapter<X3SelectCrmQuestionAdapter.SelectCrmQuestionHolder> {

    private List<CrmCarQuetionBean.SubItemList> mList;

    private Context mContext;
    private List<Boolean> sList;
    private String itemCode;
    private String itemName;
    private  List<CarQuestionSelectBean> selectList;
    private  List<Integer> posList;
    private List<Integer> addList ;
    public X3SelectCrmQuestionAdapter(Context mContext, String itemCode, String itemName) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.sList = new ArrayList<>();
        this.posList = new ArrayList<>();
        this.addList = new ArrayList<>();
        this.itemCode = itemCode;
        this.itemName = itemName;
    }

    public void changeData(List<CrmCarQuetionBean.SubItemList> mList, List<CarQuestionSelectBean> selectList) {
        this.mList = mList;
        this.sList.clear();
        this.posList.clear();
        this.selectList = selectList;//选中的结合
        for (int i = 0; i < mList.size(); i++) {
            for (int j = 0; j < selectList.size(); j++){
                if(selectList.get(j).itemCodes.equals(mList.get(i).itemCode)){//相等时的索引
                    posList.add(i);
                }
                if(selectList.get(j).itemCode.equals(itemCode) && selectList.get(j).itemCodes.equals("ALL")){
                    posList.add(i);
                }
            }
            sList.add(false);
        }
        for(int k =0; k < posList.size(); k++){
            sList.set(posList.get(k),true);
        }

        notifyDataSetChanged();
    }
    public void isSelelctAll(boolean isSelect){
        this.addList.clear();
        for (int i = 0; i < sList.size(); i++){
            if (sList.get(i) == isSelect) {
                continue;
            }
            if(isSelect){
                if(!sList.get(i)){
                    sList.remove(i);
                    sList.add(i,true);
                    addList.add(i);//记录从fasle变为true的   全选时 只返回从fale变为true 的数据
                }
            }else {
                sList.remove(i);
                sList.add(i,false);
            }

        }

        getSelectList(isSelect);

        notifyDataSetChanged();
    }

    public void getSelectList(boolean isSelect) {
        List<CarQuestionSelectBean> modelList = new ArrayList<>();
        if(isSelect){
            for (int i = 0; i < addList.size(); i++){
                if (sList.get(i) == !isSelect) {
                    continue;
                }
                CarQuestionSelectBean carModelSelectBean = new CarQuestionSelectBean();
                carModelSelectBean.itemName = itemName;
                carModelSelectBean.itemCode = itemCode;
                carModelSelectBean.itemNames = mList.get(addList.get(i)).itemName;
                carModelSelectBean.itemCodes = mList.get(addList.get(i)).itemCode;
                modelList.add(carModelSelectBean);
            }

        }else {
            for (int i = 0;i < mList.size(); i++) {
                if (sList.get(i) == !isSelect) {
                    continue;
                }
                CarQuestionSelectBean carModelSelectBean = new CarQuestionSelectBean();
                carModelSelectBean.itemName = itemName;
                carModelSelectBean.itemCode = itemCode;
                carModelSelectBean.itemNames = mList.get(i).itemName;
                carModelSelectBean.itemCodes = mList.get(i).itemCode;
                modelList.add(carModelSelectBean);
            }

        }

        if (iOnSelectStyle != null) {
            iOnSelectStyle.iOnCarModelIsSelect(modelList,isSelect,"all",itemName,itemCode);
        }
    }
    @NonNull
    @Override
    public X3SelectCrmQuestionAdapter.SelectCrmQuestionHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new X3SelectCrmQuestionAdapter.SelectCrmQuestionHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_select_car_model_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull X3SelectCrmQuestionAdapter.SelectCrmQuestionHolder holder, int position) {
        holder.tv_name.setText(mList.get(position).itemName + "");
        if (sList.get(position)) {
            holder.iv_select.setImageResource(R.mipmap.x3_edit_light_select);
        }else {
            holder.iv_select.setImageResource(R.mipmap.x3_edit_light_default);
        }
        holder.itemView.setOnClickListener(view -> {
            Boolean aBoolean = sList.get(position);
            sList.remove(position);
            sList.add(position,!aBoolean);
            CarQuestionSelectBean carQuestionSelectBean = new CarQuestionSelectBean();
            carQuestionSelectBean.itemName = itemName;
            carQuestionSelectBean.itemCode = itemCode;
            carQuestionSelectBean.itemNames = mList.get(position).itemName;
            carQuestionSelectBean.itemCodes = mList.get(position).itemCode;
            List<CarQuestionSelectBean> selectBeans = new ArrayList<>();
            selectBeans.add(carQuestionSelectBean);
            if (iOnSelectStyle != null) {
                iOnSelectStyle.iOnCarModelIsSelect(selectBeans,sList.get(position),"single",itemName,itemCode);
            }

            notifyItemChanged(position);


        });

    }
    public boolean isGroupHead(int position) {
        if (position >= mList.size()) {
            return false;
        }
        if (position == 0) {
            return true;
        } else {
            String currentGroup = getGroupName(position);
            String preGroup = getGroupName(position - 1);
            return !currentGroup.equals(preGroup);
        }
    }

    /**
     * 获取组名
     */
    public String getGroupName(int position){

        String sortLetters = mList.get(position).sortLetters;

        return "🔥".equals(sortLetters) ? mContext.getString(R.string.x3_common_brand) : sortLetters;
    }

    IOnSelectStyle iOnSelectStyle;

    public void setiOnSelectStyle(IOnSelectStyle iOnSelectStyle) {
        this.iOnSelectStyle = iOnSelectStyle;
    }

    public interface IOnSelectStyle {
        void iOnCarModelIsSelect(List<CarQuestionSelectBean> selectListAll, boolean isAdd, String type, String itemName, String itemCode);
//        void iOnCarModelSelected(CarQuestionSelectBean carQuestionSelectBean);
//        void iOnCarModelUnSelected(CarQuestionSelectBean carQuestionSelectBean);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class SelectCrmQuestionHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        ImageView iv_select;
        public SelectCrmQuestionHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_select = itemView.findViewById(R.id.iv_select);
        }
    }
}
